package cn.demob;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDataChangeListener {
    private ListView lvStu;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initAdapter();

        readDataFromDB();
        addStu();
        editStu();
    }

    /**
     * 编辑学生
     **/
    private void editStu() {
        lvStu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Student student = studentArrayList.get(position);
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                intent.putExtra("STUNO", student.getStuno());
                intent.putExtra("NAME", student.getName());
                intent.putExtra("AGE", student.getAge());
                intent.putExtra("SEX", student.getSex());
                startActivityForResult(intent, 1002);
            }
        });
    }

    /**
     * 添加学生
     * **/
    private void addStu() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();

                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });

    }


    /**
     * 获取结果码
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 2001) {
            String stuno = data.getStringExtra("STUNO");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            String sex = data.getStringExtra("SEX");

            Student student = new Student(stuno, name, age, sex);
            addStudentToDB(student);
        } else if (resultCode == 3001) {
            String stuno = data.getStringExtra("STUNO");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            String sex = data.getStringExtra("SEX");

            Student student = new Student(stuno, name, age, sex);
            updateStudentToDB(student);
        }

    }

    /**
     * 更新数据库数据
     */
    private void updateStudentToDB(Student student) {
        Uri uri = Uri.parse("content://com.gdcp.student/student");

        ContentValues values = new ContentValues();
        values.put("name", student.getName());
        values.put("age", student.getAge());
        values.put("sex", student.getSex());

        String where = "stuno = '" + student.getStuno() + "'";
        getContentResolver().update(uri, values, where, null);
        readDataFromDB();
    }

    /**
     * 添加学生信息进数据库
     */
    private void addStudentToDB(Student student) {
        Uri uri = Uri.parse("content://com.gdcp.student/student");

        ContentValues values = new ContentValues();
        values.put("stuno", student.getStuno());
        values.put("name", student.getName());
        values.put("age", student.getAge());
        values.put("sex", student.getSex());

        getContentResolver().insert(uri, values);
        readDataFromDB();
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        adapter = new StuAdapter(MainActivity.this, studentArrayList, MainActivity.this);
        lvStu.setAdapter(adapter);
    }

    /**
     * 从数据库读取学生列表
     */
    private void readDataFromDB() {
        Uri uri = Uri.parse("content://com.gdcp.student/student");
        studentArrayList.clear();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                String stuno = cursor.getString(0);
                String name = cursor.getString(1);
                int age = cursor.getInt(2);
                String sex = cursor.getString(3);

                Student student = new Student(stuno, name, age, sex);
                studentArrayList.add(student);
            }
        }

        adapter.notifyDataSetChanged();

    }

    /**
     * 绑定组件
     */
    private void findViews() {
        lvStu = findViewById(R.id.lv_stu);
        btnAdd = findViewById(R.id.btn_add);
    }

    /*
     * 删除学生
     * */
    @Override
    public void del(Student student) {
        Uri uri = Uri.parse("content://com.gdcp.student/student");
        String where = "stuno = ?";
        String[] args = {student.getStuno()};
        getContentResolver().delete(uri, where, args);
        readDataFromDB();
    }
}


